﻿using System;
using System.IO;
using System.Windows.Forms;

namespace ExtractItAll.Forms
{
  public partial class SettingsManager : UserControl
  {
    public SettingsManager()
    {
      InitializeComponent();
      InitSettings();
      UpdateControls();
    }

    private void InitSettings()
    {
      threadPriorityComboBox.Items.Clear();
      foreach (var item in Enum.GetValues(typeof (System.Threading.ThreadPriority)))
        threadPriorityComboBox.Items.Add(item);
    }

    private void UpdateControls()
    {
      subDirRadioButton.Checked = !Program.Settings.UseGlobalExtractPath;
      fixedDirRadioButton.Checked = Program.Settings.UseGlobalExtractPath;
      fixedDirTextBox.Enabled = fixedDirRadioButton.Checked;
      fixedDirBrowseButton.Enabled = fixedDirRadioButton.Checked;

      fixedDirTextBox.Text = Program.Settings.ExtractPath;
      threadPriorityComboBox.SelectedItem = Program.Settings.ThreadPriority;
      trySmallFileFirstCheckBox.Checked = Program.Settings.TrySmallFileFirst;
      minimizeToTrayCheckBox.Checked = Program.Settings.MinimizeToTray;
      updatesCheckBox.Checked = Program.Settings.UpdateCheckOnStartup;
      updateOnceADayCheckBox.Checked = Program.Settings.UpdateCheckOnlyOnceADay;
    }

    #region Controls

    private void fixedDirRadioButton_Click(object sender, EventArgs e)
    {
      if (String.IsNullOrEmpty(fixedDirTextBox.Text))
        fixedDirBrowseButton_Click(sender, e);

      if (!String.IsNullOrEmpty(fixedDirTextBox.Text))
        Program.Settings.UseGlobalExtractPath = true;

      UpdateControls();
    }

    private void fixedDirBrowseButton_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
      folderBrowserDialog.ShowNewFolderButton = true;

      if (Directory.Exists(Program.Settings.ExtractPath))
        folderBrowserDialog.SelectedPath = Program.Settings.ExtractPath;

      if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
      {
        Program.Settings.ExtractPath = folderBrowserDialog.SelectedPath;
        UpdateControls();
      }
    }

    private void subDirRadioButton_Click(object sender, EventArgs e)
    {
      Program.Settings.UseGlobalExtractPath = false;
      UpdateControls();
    }

    private void minimizeToTrayCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      Program.Settings.TrySmallFileFirst = minimizeToTrayCheckBox.Checked;
    }

    private void updatesCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      Program.Settings.UpdateCheckOnStartup = updatesCheckBox.Checked;
      updateOnceADayCheckBox.Enabled = updatesCheckBox.Checked;
    }

    private void updateOnceADayCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      Program.Settings.UpdateCheckOnlyOnceADay = updateOnceADayCheckBox.Checked;
    }

    private void trySmallFileFirstCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      Program.Settings.TrySmallFileFirst = trySmallFileFirstCheckBox.Checked;
    }

    private void threadPriorityComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      Program.Settings.ThreadPriority = (System.Threading.ThreadPriority)threadPriorityComboBox.SelectedItem;
    }

    #endregion
  }
}